//
//  TopicTableViewCell.m
//  礼物go
//
//  Created by dllo on 16/5/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TopicTableViewCell.h"
#import "TopicCollectionViewCell.h"
#import "TopicModel.h"


#define leftSpace 10
#define upSpace 10

static NSString *const topicCollCell = @"topicCollCell";

@interface TopicTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *topicCollView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;

@property(nonatomic,strong)NSMutableArray *topicCollArray;

@end

@implementation TopicTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.dataArray = [NSMutableArray array];
        self.topicCollArray = [NSMutableArray array];
        [self createTopicCollectionView];
    }
    return self;
}

-(void)createTopicCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(self.contentView.frame.size.width / 2 - leftSpace * 4, self.contentView.frame.size.height - upSpace * 2);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(upSpace, leftSpace, upSpace, leftSpace);
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.topicCollView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:self.flowLayout];
self.topicCollView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
    [self.contentView addSubview:self.topicCollView];
    self.topicCollView.delegate = self;
    self.topicCollView.dataSource = self;
    [self.topicCollView registerClass:[TopicCollectionViewCell class] forCellWithReuseIdentifier:topicCollCell];
    self.topicCollView.showsHorizontalScrollIndicator = NO;
    [self getTopicData];
    
}

-(void)getTopicData{
NSString *urlStr = @"http://api.liwushuo.com/v2/collections?limit=6&offset=0";
    [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSMutableArray *collectionsArray = [dataDic objectForKey:@"collections"];
        for (NSDictionary *dic in collectionsArray) {
            TopicModel *topicM = [[TopicModel alloc] init];
            [topicM setValuesForKeysWithDictionary:dic];
            [self.topicCollArray addObject:topicM];
        }
        [self.topicCollView reloadData];
        
    } error:^(NSError *error) {
        
        
    }];

}



-(void)layoutSubviews{
    [super layoutSubviews];
    self.flowLayout.itemSize = CGSizeMake(self.contentView.frame.size.width / 2 - leftSpace * 4, self.contentView.frame.size.height - upSpace * 2);
    self.topicCollView.frame = self.contentView.bounds;

}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TopicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:topicCollCell forIndexPath:indexPath];
    TopicModel *topicM = [self.topicCollArray objectAtIndex:indexPath.row];
    cell.topicModel = topicM;
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 5;
   // cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.topicCollArray.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TopicModel *topicM = [self.topicCollArray objectAtIndex:indexPath.row];
    NSString *data = [NSString stringWithFormat:@"%@",topicM.myId];
    self.topicBlock(data);

}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
